package com.xie.smfs.client.data;

import com.xie.smfs.client.screen.GhostControlScreen;
import net.minecraft.client.MinecraftClient;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtIo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ClientDataManager {
    private static final String DATA_FILE_NAME = "smfs_client_data.nbt";
    private static NbtCompound clientData;

    // 初始化客户端数据
    public static void init() {
        clientData = loadClientData();
        if (clientData == null) {
            clientData = new NbtCompound();
            // 初始化默认数据
            clientData.putInt("tutorial_view_count", 0);
        clientData.putBoolean("first_night_curse_triggered", false);
        clientData.putBoolean("has_received_starter_items", false);
        clientData.putString("shown_events", "[]");
            saveClientData();
        }
    }

    // 加载客户端数据
    private static NbtCompound loadClientData() {
        MinecraftClient client = MinecraftClient.getInstance();
        if (client == null) return null;

        File dataFile = new File(client.runDirectory, DATA_FILE_NAME);
        if (!dataFile.exists()) return null;

        try (FileInputStream fis = new FileInputStream(dataFile)) {
            return NbtIo.readCompressed(fis);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 保存客户端数据
    public static void saveClientData() {
        MinecraftClient client = MinecraftClient.getInstance();
        if (client == null || clientData == null) return;

        File dataFile = new File(client.runDirectory, DATA_FILE_NAME);
        try (FileOutputStream fos = new FileOutputStream(dataFile)) {
            NbtIo.writeCompressed(clientData, fos);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 获取教程查看次数
    public static int getTutorialViewCount() {
        return clientData.getInt("tutorial_view_count");
    }

    // 增加教程查看次数
    public static void incrementTutorialViewCount() {
        clientData.putInt("tutorial_view_count", getTutorialViewCount() + 1);
        saveClientData();
    }

    // 获取已显示事件
    public static String getShownEvents() {
        return clientData.getString("shown_events");
    }

    // 设置已显示事件
    public static void setShownEvents(String events) {
        clientData.putString("shown_events", events);
        saveClientData();
    }



    // 同步服务器发送的玩家数据到客户端
    public static void syncPlayerData(NbtCompound data) {
        if (clientData == null) {
            init();
        }
        // 合并服务器数据到客户端
        clientData.copyFrom(data);
        saveClientData();
        // 触发UI刷新
        MinecraftClient.getInstance().execute(() -> {
            if (MinecraftClient.getInstance().currentScreen instanceof GhostControlScreen) {
                GhostControlScreen screen = (GhostControlScreen) MinecraftClient.getInstance().currentScreen;
                // No markDirty() call as we removed this method
            }
        });
    }


    public static boolean isFirstNightCurseTriggered() {
        return clientData.getBoolean("first_night_curse_triggered");
    }

    public static void setFirstNightCurseTriggered(boolean triggered) {
        clientData.putBoolean("first_night_curse_triggered", triggered);
        saveClientData();
    }

    public static boolean hasReceivedStarterItems() {
        return clientData.getBoolean("has_received_starter_items");
    }

    public static void setHasReceivedStarterItems(boolean received) {
        clientData.putBoolean("has_received_starter_items", received);
        saveClientData();
    }


}